package cn.com.taiji.pos.controller;

import cn.com.taiji.pos.entity.ConfigMealTime;
import cn.com.taiji.pos.entity.MenuFoodItem;
import cn.com.taiji.pos.service.IConfigMealTimeService;
import cn.com.taiji.pos.service.IMenuFoodItemService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 小卖部
 */
@RestController
@RequestMapping("/Commissary")
public class CommissaryController {
    @Value("${FOOD_CATEGORY_ID_CODE_xmb}")
    private String FOOD_CATEGORY_ID_CODE_xmb;

    @Autowired
    private IMenuFoodItemService menuFoodItemService;
    @Autowired
    private IConfigMealTimeService configMealTimeService;
//    private Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//校验数字

    /**
     * 小卖部
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String search(Model model, HttpServletRequest request, HttpServletResponse response) {
        String barCode = request.getParameter("barCode");
        List<MenuFoodItem> foodItemList = menuFoodItemService.listXMBFood(barCode, FOOD_CATEGORY_ID_CODE_xmb);
        if (foodItemList.size() > 0) {
            model.addAttribute("result", "success");
        } else {
            model.addAttribute("result", "false");
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss");
        QueryWrapper<ConfigMealTime> configMealTimeWrapper = new QueryWrapper<>();
        configMealTimeWrapper.le("START_TIME", df.format(LocalTime.now())).ge("END_TIME", df.format(LocalTime.now())).orderByAsc("SORT_NO");
        ConfigMealTime configMealTime = configMealTimeService.getOne(configMealTimeWrapper, false);
        model.addAttribute("foodItemList", foodItemList);
        model.addAttribute("canci", configMealTime.getName());
        return JSONObject.toJSONString(model);
    }


}
